package edu.cmu.cs.webapp.ioms.ui.wizards.formula;

import java.util.ArrayList;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;

import edu.cmu.cs.webapp.ioms.models.FormularRecord;
import edu.cmu.cs.webapp.ioms.models.IOMSFactory;
import edu.cmu.cs.webapp.ioms.models.Ingredient;
import edu.cmu.cs.webapp.ioms.ui.dialogs.formula.BrowseIngredientDialog;
import edu.cmu.cs.webapp.ioms.utils.SWTUtils;

public class FormulaMainWizard extends WizardPage {

	private Text _formulaID;
	private Text _ingredientName;
	// need to decide later whether to include another pop-up box for this
	// functionality
	// private Text _quantity;

	private Spinner _quantity;

	private FormularRecord returnFr;
	private Ingredient ing;

	protected FormulaMainWizard(String pageName) {
		super(pageName);
		setTitle("Formula Main");
		setDescription("Please input the information for the current formuula");
	}

	public FormulaMainWizard(String pageName, String title,
			ImageDescriptor titleImage) {
		super(pageName, title, titleImage);
	}

	@Override
	public void createControl(Composite parent) {
		// direct container under parent
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		container.setLayout(layout);

		// layout.verticalSpacing = 9;

		Label idLabel = new Label(container, SWT.BEGINNING);
		idLabel.setText("Formular ID:");
		// idLabel.setLayoutData(gd);
		_formulaID = new Text(container, SWT.BORDER | SWT.SINGLE | SWT.FILL);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		_formulaID.setLayoutData(gd);
		_formulaID.setEditable(false);
		_formulaID.setEnabled(false);

		// set the first column in the contianer, usually is the name of the
		// input
		Label label = new Label(container, SWT.BEGINNING);
		label.setText("Ingredient Name:"); //$NON-NLS-1$
		_ingredientName = new Text(container, SWT.BORDER | SWT.SINGLE
				| SWT.FILL);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 1;
		_ingredientName.setLayoutData(gd);

		this._ingredientName.setEditable(false);
		this._ingredientName.setEnabled(false);

		Button browseBtn = SWTUtils.createPushButton(container, "Browse...");
		gd = new GridData();
		gd.horizontalSpan = 1;
		browseBtn.setLayoutData(gd);
		browseBtn.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				BrowseIngredientDialog dlg = new BrowseIngredientDialog(
						getShell());
				if (dlg.open() == IDialogConstants.OK_ID) {
					ing = dlg.getSelectedModel();
					_ingredientName.setText(" " + ing.getIng_id() + "(#"
							+ ing.getName() + ")");
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub

			}
		});

		// create a line between the product name and other element, deliminator
		createLine(container, layout.numColumns);

		label = new Label(container, SWT.BEGINNING);
		label.setText("Ingredient Quantity:"); //$NON-NLS-1$

		_quantity = new Spinner(container, SWT.BORDER | SWT.WRAP);
		_quantity.setMaximum(10000);
		_quantity.setMinimum(0);

		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		_quantity.setLayoutData(gd);

		_quantity.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		if (isUpdate()) {
			loadFromExistingModel();
		}

		setControl(container);
		validate();
		container.pack(true);
	}

	private void loadFromExistingModel() {
		// put the selected information into the pop-ups, this model is the one
		// formula record
		FormularRecord model = getModel();

		_formulaID.setText("" + model.getForm_id());
		_ingredientName.setText(model.getIngredient().getName());
		_quantity.setSelection((int) model.getIng_std_quantity());

	}

	private FormularRecord getModel() {
		return ((FormularWizard) getWizard()).getModel();
	}

	private void createLine(Composite container, int numColumns) {
		Label line = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL
				| SWT.BOLD);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = numColumns;
		line.setLayoutData(gridData);

	}

	private boolean validate() {
		
		boolean valid = true;
		return valid;
		
	}

	private boolean isUpdate() {
		return ((FormularWizard) getWizard()).isUpdate();
	}

	public FormularRecord getFormRecord() {
		returnFr = IOMSFactory.eINSTANCE.createFormularRecord();
		if (_formulaID.getText().trim().length() != 0) {
			returnFr.setForm_id(Integer.parseInt(_formulaID.getText().trim()));
		}
		returnFr.setIngredient(ing);
		returnFr.setIng_std_quantity(_quantity.getSelection());
		return returnFr;

	}

}
